<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:rwt="http://schema.pbs.gov.au/RWT/">

  <xsl:import href='common.xsl'/>

  <db:article>
    <db:info>
      <db:title>Extract Notes File</db:title>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.2</db:revnumber>
          <db:date>2013-07-05</db:date>
          <db:authorinitials>JPK</db:authorinitials>
          <db:revremark>Remove rwt:prescriber-instruction content from notes.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1.2</db:revnumber>
          <db:date>2013-03-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed rwt:definition.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1.1</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2012-10-22</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Remove constraints notes.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.5</db:revnumber>
          <db:date>2012-07-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Performance optimisation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.4</db:revnumber>
          <db:date>2011-11-03</db:date>
          <db:authorinitials>BD</db:authorinitials>
          <db:revremark>Exclude notes with non-numeric codes.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.3</db:revnumber>
          <db:date>2011-10-26</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Strip spaces from source.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.2</db:revnumber>
          <db:date>2011-10-25</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed field padding... again.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.1</db:revnumber>
          <db:date>2011-06-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed field padding.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-02-18</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML Schema v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-10-01</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the Notes ANS extract file (<db:filename>NoteExtract yyyymmdd.txt</db:filename>) from a PBS XML document.</db:para>
    <db:para>The Note extract lists all Note ids and their descriptions. It is used for linking to values in the LinkExtract and PBS Item Table extract files.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The field delimeter in this file is the tab character.</db:para>
    <db:table>
      <db:tgroup cols="5" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:colspec colnum="4" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Start Position</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Notes Id</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>4</db:entry>
            <db:entry>1</db:entry>
            <db:entry>Numeric characters</db:entry>
          </db:row>
          <db:row>
            <db:entry>Notes Text</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>12600</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Text of Note description from the PBS Book.</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method='text'/>
  <xsl:strip-space elements='*'/>

  <xsl:key name='note'
    match='/pbs:root/rwt:restrictions-list//rwt:administrative-advice |
           /pbs:root/rwt:restrictions-list//rwt:definition'
    use='pbs:code'/>

  <db:template name='root'>
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML Schema.</db:para>
  </db:template>
  <xsl:template match='/'>
    <xsl:variable name='major' select='substring-before(/*/@version, ".")'/>

    <xsl:choose>
      <xsl:when test='$major >= 2'>
        <!-- Eliminate duplicates -->
        <xsl:variable name='notes'
          select='/pbs:root/rwt:restrictions-list//rwt:administrative-advice |
                  /pbs:root/rwt:restrictions-list//rwt:definition'/>

        <xsl:apply-templates
          select='$notes[count(.|key("note", pbs:code)[1]) = 1]'>
          <xsl:sort select='pbs:code' order='ascending' data-type='number'/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate='yes'>This stylesheet is designed to operate upon a Schedule XML document that conforms to version 2.4 (or later) of the PBS XML Schema.  See http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of this XSL stylesheet that operates on documents that conform to an earlier version of the schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='rwt:administrative-advice |
    rwt:definition'>
    <xsl:variable name='text'>
      <xsl:apply-templates select='*[not(self::pbs:code|self::rwt:constraints)]'/>
    </xsl:variable>

    <xsl:if test='pbs:code and
                  pbs:code != "0" and
                  pbs:code != "" and
                  translate(pbs:code,"0123456789","") = "" and
                  normalize-space($text) != "" and
                  not(rwt:constraints/* and contains(db:note, "Flag Set"))'>
      <xsl:call-template name='pad-number'>
        <xsl:with-param name='number' select='pbs:code'/>
        <xsl:with-param name='length' select='4'/>
        <xsl:with-param name='pad' select='"0"'/>
      </xsl:call-template>
      <xsl:text>	</xsl:text>
      <xsl:call-template name='truncate'>
        <xsl:with-param name='text' select='$text'/>
        <xsl:with-param name='length' select='12600'/>
      </xsl:call-template>
      <xsl:text>&#xa;</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match='*'>
    <xsl:if test='parent::*[self::rwt:administrative-advice|self::rwt:definition] and
                  preceding-sibling::*[not(self::pbs:code|self::rwt:constraints)]'>
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>
